/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.shinyray.coa.view.user;

import com.icesoft.faces.component.dragdrop.DndEvent;
import com.icesoft.faces.component.dragdrop.DragEvent;
import com.icesoft.faces.component.ext.HtmlPanelGroup;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Logger;

/**
 * 用户管理
 * @author alain
 */
public class UserAdminBean {
    private List<UserVO> users;
    private Logger logger=Logger.getLogger(UserAdminBean.class.getName());
    private List<String> currentUserRoles=new ArrayList<String>();
    private List<String> currentUserAvaliableRoles=new ArrayList<String>();
    private UserVO newUser=new UserVO();
    public UserAdminBean() {
        this.init();
    }

    public List<UserVO> getUsers() {
        return users;
    }

    public void setUsers(List<UserVO> users) {
        this.users = users;
    }

    public UserVO getNewUser() {
        return newUser;
    }

    public void setNewUser(UserVO newUser) {
        this.newUser = newUser;
    }

    public List<String>  getCurrentUserAvaliableRoles() {
        return currentUserAvaliableRoles;
    }

    public void setCurrentUserAvaliableRoles(List<String>  currentUserAvaliableRoles) {
        this.currentUserAvaliableRoles = currentUserAvaliableRoles;
    }

    public List<String> getCurrentUserRoles() {
        return currentUserRoles;
    }

    public void setCurrentUserRoles(List<String> currentUserRoles) {
        this.currentUserRoles = currentUserRoles;
    }



    public void userSelectListener(RowSelectorEvent event)
    {
//        //logger.info("rowID:"+event.getRow());
//        int i=0;
//        UserVO user;
//        for(i=0;i<this.users.size();i++)
//        {
//            user=this.users.get(i);
//        }
    }
    public String removeUserAction()
    {
        int i=0;
        UserVO user;
        for(i=0;i<this.users.size();i++)
        {
            user=this.users.get(i);
            if(user.isSelected())
            {
                this.users.remove(user);
                //TODO: 需要从数据库中删除
            }
            
        }
        return "";
    }

    public String queryUserAction()
    {
        //TODO:补充从数据库中查询的逻辑
        return "";
    }
    public String addUserAction()
    {
        this.users.add(0, newUser);
        //TODO:检查用户名是否存在，进行数据中保存
        return "user/admin/list/view";
    }
    /**
     * 为用户增加角色定义
     * @param event
     */
    public void addRoleToUser(DragEvent event)
    {
        //只处理已经Dropped的事件

        if(event.getEventType()==DndEvent.DROPPED)
        {
            String targetId = event.getTargetClientId();
            if(targetId!=null)
            {
                String role=(String)((HtmlPanelGroup) event.getComponent()).getDragValue();
                if(!this.currentUserRoles.contains(role))
                {
                    this.currentUserRoles.add(role);
                    this.currentUserAvaliableRoles.remove(role);
                }//end if
            }//end if
        }
    }
    public void removeRoleFromUser(DragEvent event)
    {
        //只处理已经Dropped的事件
        if(event.getEventType()==DndEvent.DROPPED)
        {
            String targetId=event.getTargetClientId();
            if(targetId!=null)
            {
                String role=(String) ((HtmlPanelGroup) event.getComponent()).getDragValue();
                if(this.currentUserRoles.contains(role))
                {
                    this.currentUserRoles.remove(role);
                    this.currentUserAvaliableRoles.add(role);
                }
            }//end if

        }
    }
    private void init()
    {
        this.users=new ArrayList<UserVO>();;
        int i=0;
        for(i=0;i<20;i++)
        {
            UserVO user=new UserVO();
            user.setAccountName("account_"+i);
            user.setDisplayName("alain_"+i);
            user.setOrgName("ibm");
            user.setSelected(false);
            List<String> roles=new ArrayList();
            for(int j=0;j<3;j++)
            {
                roles.add("role_"+j);
            }
            user.setRoles(roles);
            
            users.add(user);
        }
        //TODO:从数据库中初始化角色
        for(i=0;i<5;i++)
        {
            String role="role_"+i;
            this.currentUserAvaliableRoles.add(role);
        }
    }
}
